﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace 使用XmlTextReader
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void btnReadAll_Click(object sender, EventArgs e)
        {
            XmlTextReader reader = new XmlTextReader(@"..\..\Book.xml");
            List<BookModel> modelList = new List<BookModel>();
            BookModel model = new BookModel();
            while (reader.Read())
            {
               
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "book")
                    {
                    //    for (int i = 0; i < reader.AttributeCount; i++)
                    //    {
                    //        reader.MoveToAttribute(i);
                    //        string str = "属性：" + reader.Name + "=" + reader.Value;
                    //    }
                        model.BookType = reader.GetAttribute(0);
                        model.BookISBN = reader.GetAttribute(1);
                    }
                    if (reader.Name == "title")
                    {
                        model.BookName=reader.ReadElementString().Trim();
                    }
                    if (reader.Name == "author")
                    {
                        model.BookAuthor = reader.ReadElementString().Trim();
                    }
                    if (reader.Name == "price")
                    {
                        model.BookPrice = Convert.ToDouble(reader.ReadElementString().Trim());
                    }
                }

                if (reader.NodeType == XmlNodeType.EndElement)
                {
                    modelList.Add(model);
                    model = new BookModel();
                }


                
            }
            modelList.RemoveAt(modelList.Count-1);
            this.dgvBookInfo.DataSource = modelList;

             //case XmlNodeType.Element:
             //           Response.Write("节点名称：" + xmlReader.LocalName+"<br>");
             //           for (int i = 0; i < xmlReader.AttributeCount; i++)
             //           {
             //               xmlReader.MoveToAttribute(i);
             //               Response.Write("属性：" + xmlReader.Name + "=" + xmlReader.Value+"&nbsp;");
             //           }


        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            XmlTextWriter myXmlTextWriter = new XmlTextWriter(@"..\..\Book1.xml", null);
            //使用 Formatting 属性指定希望将 XML 设定为何种格式。 这样，子元素就可以通过使用 Indentation 和 IndentChar 属性来缩进。
            myXmlTextWriter.Formatting = Formatting.Indented;

            myXmlTextWriter.WriteStartDocument(false);
            myXmlTextWriter.WriteStartElement("bookstore");

            myXmlTextWriter.WriteComment("记录书本的信息");
            myXmlTextWriter.WriteStartElement("book");

            myXmlTextWriter.WriteAttributeString("Type", "选修课");
            myXmlTextWriter.WriteAttributeString("ISBN", "111111111");

            myXmlTextWriter.WriteElementString("author","张三");
            myXmlTextWriter.WriteElementString("title", "职业生涯规划");
            myXmlTextWriter.WriteElementString("price", "16.00");

            myXmlTextWriter.WriteEndElement();
            myXmlTextWriter.WriteEndElement();

            myXmlTextWriter.Flush();
            myXmlTextWriter.Close();
           



        }
    }
}
